Skip to content

Fix use-after-free during reentrant spawn_future cancellation - #2535

Open
Waterpine wants to merge 2 commits into
NVIDIA:mainfrom
Waterpine:fix/spawn-future-cancel
Open

Waterpine wants to merge 2 commits into
NVIDIA:mainfrom
Waterpine:fix/spawn-future-cancel

Conversation

@Waterpine

Copy link
Copy Markdown

stdexec::spawn_future can access freed state when a consumer's stop request causes the underlying sender to complete synchronously.

__do_try_cancel calls the internal stop source's request_stop(). During that call, the producer can complete, notify the consumer, and destroy the shared state. When request_stop() returns, the cancellation function still accesses __registered_receiver_, resulting in a heap-use-after-free. This is reproducible on a single thread.

Add a lifetime reference held for the duration of the cancellation callback. Normal cleanup releases its reference, and a scope guard releases the callback's temporary reference on exit. The shared state is destroyed only after both references have been released.

Testing

  • Reproduced the original heap-use-after-free with AddressSanitizer.
  • Added regression coverage for synchronous completion during cancellation, deferred completion, and a consumer stopped before start.
  • All 13 related test cases passed with AddressSanitizer and UndefinedBehaviorSanitizer on Linux x86-64 with GCC 13.
  • Passed the repository-wide clang-format 21 check, including the new regression test file.

@copy-pr-bot

copy-pr-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test f0d17db

nvc++ >= 26.3 configures stdexec into std::execution, so the stdexec::
namespace does not exist in the GPU CI builds. Use the STDEXEC macro
like the rest of the test suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test 4a8ebbd

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants